package com.swpu.dao.impl;

import dao.VehicleDao;
import pojo.Vehicle;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class VehicleDaoImpl implements VehicleDao {
    @Override
    public int addVehicle(Vehicle vehicle) {
        String sql = "INSERT INTO vehicles (license_plate, brand, type, is_rented, rent_days) VALUES (?,?,?,?,?)";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, vehicle.getLicensePlate());
            pstmt.setString(2, vehicle.getBrand());
            pstmt.setString(3, vehicle.getType());
            pstmt.setBoolean(4, vehicle.isRented());
            pstmt.setInt(5, vehicle.getRentDays());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Vehicle getVehicleByLicensePlate(String licensePlate) {
        String sql = "SELECT * FROM vehicles WHERE license_plate =?";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, licensePlate);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                Vehicle vehicle = new Vehicle();
                vehicle.setId(rs.getInt("id"));
                vehicle.setLicensePlate(rs.getString("license_plate"));
                vehicle.setBrand(rs.getString("brand"));
                vehicle.setType(rs.getString("type"));
                vehicle.setRented(rs.getBoolean("is_rented"));
                vehicle.setRentDays(rs.getInt("rent_days"));
                return vehicle;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateVehicle(Vehicle vehicle) {
        String sql = "UPDATE vehicles SET brand =?, is_rented =?, rent_days =? WHERE license_plate =?";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, vehicle.getBrand());
            pstmt.setBoolean(2, vehicle.isRented());
            pstmt.setInt(3, vehicle.getRentDays());
            pstmt.setString(4, vehicle.getLicensePlate());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int deleteVehicle(String licensePlate) {
        String sql = "DELETE FROM vehicles WHERE license_plate =?";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, licensePlate);
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public List<Vehicle> getAllVehicles() {
        List<Vehicle> vehicleList = new ArrayList<>();
        String sql = "SELECT * FROM vehicles";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                Vehicle vehicle = new Vehicle();
                vehicle.setId(rs.getInt("id"));
                vehicle.setLicensePlate(rs.getString("license_plate"));
                vehicle.setBrand(rs.getString("brand"));
                vehicle.setType(rs.getString("type"));
                vehicle.setRented(rs.getBoolean("is_rented"));
                vehicle.setRentDays(rs.getInt("rent_days"));
                vehicleList.add(vehicle);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return vehicleList;
    }

    @Override
    public int calculateTurnover() {
        String sql = "SELECT SUM(CASE WHEN type = 'Car' THEN 50 * rent_days " +
                "WHEN type = 'Truck' THEN CASE WHEN rent_days <= 5 THEN 100 * rent_days ELSE 100 * 5 + 95 * (rent_days - 5) END " +
                "WHEN type = 'Bus' THEN CASE WHEN rent_days <= 5 THEN 80 * rent_days ELSE 80 * 5 + 80 * 0.8 * (rent_days - 5) END END) " +
                "FROM vehicles WHERE is_rented = true";
        try (Connection conn = dao.impl.DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}